<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检测变量是否为数字或数字字符串</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.is-null.html">« is_null</a></li>
      <li style="float: right;"><a href="function.is-object.html">is_object »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.var.html">Variable handling 函数</a></li>
    <li>检测变量是否为数字或数字字符串</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.is-numeric" class="refentry">
 <div class="refnamediv">
     <h1 class="refname">is_numeric</h1>
     <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">is_numeric</span> &mdash; <span class="dc-title">
      检测变量是否为数字或数字字符串
     </span></p>

 </div>

  <div class="refsect1 description" id="refsect1-function.is-numeric-description">
    <h3 class="title">说明</h3>
    <div class="methodsynopsis dc-description">
     <span class="methodname"><strong>is_numeric</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type">bool</span></div>

    <p class="para rdfs-comment">
     检测指定的变量是否为数字或<a href="language.types.numeric-strings.html" class="link">数字字符串</a>。
    </p>
  </div>


  <div class="refsect1 parameters" id="refsect1-function.is-numeric-parameters">
   <h3 class="title">参数</h3>
   <p class="para">
    <dl>
     
      <dt>
<code class="parameter">value</code></dt>

      <dd>

       <p class="para">
        需要检测的变量。
       </p>
      </dd>

     
    </dl>

   </p>
  </div>


  <div class="refsect1 returnvalues" id="refsect1-function.is-numeric-returnvalues">
   <h3 class="title">返回值</h3>
   <p class="para">
    如果 <code class="parameter">value</code> 是数字或<a href="language.types.numeric-strings.html" class="link">数字字符串</a>，
    返回 <strong><code>true</code></strong>，否则返回 <strong><code>false</code></strong>。
   </p>
  </div>


 <div class="refsect1 changelog" id="refsect1-function.is-numeric-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       以空格结尾的数字字符串（<code class="literal">&quot;42 &quot;</code>）现在将返回 <strong><code>true</code></strong>。以前，会返回 <strong><code>false</code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.is-numeric-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5290">
    <p><strong>示例 #1 <span class="function"><strong>is_numeric()</strong></span> 示例</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$tests&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"42"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1337</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">0x539</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">02471</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">0b10100111001</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1337e0</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"0x539"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"02471"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"0b10100111001"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"1337e0"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"not&nbsp;numeric"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;array(),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">9.1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,<br />);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$tests&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$element</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;is&nbsp;numeric"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;is&nbsp;NOT&nbsp;numeric"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
     <div class="cdata"><pre>
&#039;42&#039; is numeric
1337 is numeric
1337 is numeric
1337 is numeric
1337 is numeric
1337.0 is numeric
&#039;0x539&#039; is NOT numeric
&#039;02471&#039; is numeric
&#039;0b10100111001&#039; is NOT numeric
&#039;1337e0&#039; is numeric
&#039;not numeric&#039; is NOT numeric
array (
) is NOT numeric
9.1 is numeric
NULL is NOT numeric
&#039;&#039; is NOT numeric
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5291">
    <p><strong>示例 #2 带有空格的 <span class="function"><strong>is_numeric()</strong></span></strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$tests&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"&nbsp;42"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"42&nbsp;"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"\u{A0}9001"</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;non-breaking&nbsp;space<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"9001\u{A0}"</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;non-breaking&nbsp;space<br /></span><span style="color: #007700">];<br />foreach&nbsp;(</span><span style="color: #0000BB">$tests&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$element</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;is&nbsp;numeric"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;is&nbsp;NOT&nbsp;numeric"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
    <div class="example-contents screen">
     <div class="cdata"><pre>
&#039; 42&#039; is numeric
&#039;42 &#039; is numeric
&#039; 9001&#039; is NOT numeric
&#039;9001 &#039; is NOT numeric
</pre></div>
    </div>
    <div class="example-contents"><p>以上例程在 PHP 7 中的输出：</p></div>
    <div class="example-contents screen">
     <div class="cdata"><pre>
&#039; 42&#039; is numeric
&#039;42 &#039; is NOT numeric
&#039; 9001&#039; is NOT numeric
&#039;9001 &#039; is NOT numeric
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.is-numeric-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="language.types.numeric-strings.html" class="link">数字字符串</a></li>
    <li class="member"><span class="function"><a href="function.ctype-digit.html" class="function" rel="rdfs-seeAlso">ctype_digit()</a> - 检测数字字符</span></li>
    <li class="member"><span class="function"><a href="function.is-bool.html" class="function" rel="rdfs-seeAlso">is_bool()</a> - 检测变量是否是布尔值</span></li>
    <li class="member"><span class="function"><a href="function.is-null.html" class="function" rel="rdfs-seeAlso">is_null()</a> - 检测变量是否是 null</span></li>
    <li class="member"><span class="function"><a href="function.is-float.html" class="function" rel="rdfs-seeAlso">is_float()</a> - 查找变量的类型是否是浮点型</span></li>
    <li class="member"><span class="function"><a href="function.is-int.html" class="function" rel="rdfs-seeAlso">is_int()</a> - 检测变量是否是整数</span></li>
    <li class="member"><span class="function"><a href="function.is-string.html" class="function" rel="rdfs-seeAlso">is_string()</a> - 查找变量的类型是否是字符串</span></li>
    <li class="member"><span class="function"><a href="function.is-object.html" class="function" rel="rdfs-seeAlso">is_object()</a> - 查找变量是否是字符串</span></li>
    <li class="member"><span class="function"><a href="function.is-array.html" class="function" rel="rdfs-seeAlso">is_array()</a> - 检测变量是否是数组</span></li>
    <li class="member"><span class="function"><a href="function.filter-var.html" class="function" rel="rdfs-seeAlso">filter_var()</a> - 使用特定的过滤器过滤一个变量</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>